package com.wu.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class HttpUtil {

	/**
	 * 向指定URL发送GET方法的请求
	 * 
	 * @param url
	 *            发送请求的URL
	 * @param param
	 *            请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
	 * @return URL 所代表远程资源的响应结果
	 */
	public static String sendGet(String urlString) {
		String body = "";
		HttpURLConnection httpUrlConn = null;
		InputStreamReader inputStreamReader = null;
		// System.out.println(monitorInfo.getDomainUrl()+"====");
		try {
			URL url = new URL(urlString);
			httpUrlConn = (HttpURLConnection) url.openConnection();
			httpUrlConn.setRequestProperty("Accept-Charset", "utf-8");
			httpUrlConn.setRequestProperty("Content-Type", "application/json");
			httpUrlConn.setRequestProperty("Connection", "keep-alive");
			httpUrlConn.setDoOutput(true); // http正文内，因此需要设为true, 默认情况下是false;
			httpUrlConn.setDoInput(true); // 设置是否从httpUrlConnection读入，默认情况下是true;
			httpUrlConn.setUseCaches(false); // Post 请求不能使用缓存
			httpUrlConn.setRequestMethod("GET");
			httpUrlConn.connect();
			// 将返回的输入流转换成字符串
			inputStreamReader = new InputStreamReader(httpUrlConn.getInputStream(), "UTF-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			String str = null;
			StringBuffer buffer = new StringBuffer();
			while ((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}
			body = buffer.toString();
			// String gbkStr = new String(body.getBytes("ISO8859-1"),"UTF-8");
			//body=decodeUnicode(body);
			//System.out.println(body);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		// 使用finally块来关闭输入流
		finally {
			try {
				inputStreamReader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return body;
	}

	public static String sendGetIO(String urlString) {
		String body = "";
		HttpURLConnection httpUrlConn = null;
		InputStreamReader inputStreamReader = null;
		// System.out.println(monitorInfo.getDomainUrl()+"====");
		try {
			URL url = new URL(urlString);
			httpUrlConn = (HttpURLConnection) url.openConnection();
			httpUrlConn.setRequestProperty("Accept-Charset", "utf-8");
			httpUrlConn.setRequestProperty("Content-Type", "application/json");
			httpUrlConn.setRequestProperty("Connection", "keep-alive");
			httpUrlConn.setDoOutput(true); // http正文内，因此需要设为true, 默认情况下是false;
			httpUrlConn.setDoInput(true); // 设置是否从httpUrlConnection读入，默认情况下是true;
			httpUrlConn.setUseCaches(false); // Post 请求不能使用缓存
			httpUrlConn.setRequestMethod("GET");
			httpUrlConn.connect();
			// 将返回的输入流转换成字符串
			inputStreamReader = new InputStreamReader(httpUrlConn.getInputStream(), "UTF-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			String str = null;
			StringBuffer buffer = new StringBuffer();
			while ((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
				System.out.println(buffer);
			}
			File videoFile = new File("d:/2.mp4");

			FileOutputStream outStream = new FileOutputStream(videoFile);

			outStream.write(buffer.toString().getBytes());

			outStream.close();

			// body=buffer.toString();
		} catch (Exception e) {
			// TODO: handle exception
		}
		// 使用finally块来关闭输入流
		finally {
			try {
				inputStreamReader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return body;
	}

	public static String decodeUnicode(String theString) {

		char aChar;

		int len = theString.length();

		StringBuffer outBuffer = new StringBuffer(len);

		for (int x = 0; x < len;) {

			aChar = theString.charAt(x++);

			if (aChar == '\\') {

				aChar = theString.charAt(x++);

				if (aChar == 'u') {

					// Read the xxxx

					int value = 0;

					for (int i = 0; i < 4; i++) {

						aChar = theString.charAt(x++);

						switch (aChar) {

						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							value = (value << 4) + aChar - '0';
							break;
						case 'a':
						case 'b':
						case 'c':
						case 'd':
						case 'e':
						case 'f':
							value = (value << 4) + 10 + aChar - 'a';
							break;
						case 'A':
						case 'B':
						case 'C':
						case 'D':
						case 'E':
						case 'F':
							value = (value << 4) + 10 + aChar - 'A';
							break;
						default:
							throw new IllegalArgumentException("Malformed   \\uxxxx   encoding.");
						}

					}
					outBuffer.append((char) value);
				} else {
					if (aChar == 't')
						aChar = '\t';
					else if (aChar == 'r')
						aChar = '\r';

					else if (aChar == 'n')

						aChar = '\n';

					else if (aChar == 'f')

						aChar = '\f';

					outBuffer.append(aChar);

				}

			} else

				outBuffer.append(aChar);

		}

		return outBuffer.toString();

	}
	
	public static String Unicode(final String dataStr) {   
        int start = 0;   
          int end = 0;   
         final StringBuffer buffer = new StringBuffer();   
          while (start > -1) {   
             end = dataStr.indexOf("\\u", start + 2);   
              String charStr = "";   
              if (end == -1) {   
                  charStr = dataStr.substring(start + 2, dataStr.length());   
             } else {   
                 charStr = dataStr.substring(start + 2, end);   
              }   
              char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。   
            buffer.append(new Character(letter).toString());   
            start = end;   
          }   
          return buffer.toString();   
      }   
}
